﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Paltop_DatabaseLayer;
using System.Web.Script.Serialization;

namespace Paltop_.Controllers
{
    public class CalendarController
    {
        private PaltopDatabaseEntities db = new PaltopDatabaseEntities();
        public int NewEvent(String Data, int accountID)
        {
            Event n = getEventFromString(Data);
            n.AccountID = accountID;
            try
            {
                db.Events.AddObject(n);
                db.SaveChanges();
                return 1;
            }
            catch (Exception)
            {

                return -1;
            }
        }
        public int EditEvent(String Data, int accountID)
        {
            Event n = getEventFromString(Data);
            n.AccountID = accountID;
            try
            {
                var query = db.Events.Where(p => p.EventID == n.EventID).FirstOrDefault();
                query.EventTypeID = n.EventTypeID;
                query.Location = n.Location;
                query.Desc = n.Desc;
                query.EndDate = n.EndDate;
                query.StartDate = n.StartDate;
                query.Type = n.Type;


                db.SaveChanges();
                return 1;
            }
            catch (Exception)
            {

                return -1;
            }
        }

        public Event getEventFromString(String Data)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Event E = js.Deserialize<Event>(Data);
            return E;

        }
        public int Delete(int eventID)
        {
            try
            {
                var ev = db.Events.Where(p => p.EventID == eventID).ToArray();
                db.Events.DeleteObject(ev.First());
                db.SaveChanges();
                return 1;
            }
            catch (Exception)
            {

                return -1;
            }

        }

        public String GetEventbyID(int eventID)
        {
            try
            {
                var ev = db.Events.Where(p => p.EventID == eventID);
                var evs = ev.Select(p => new { p.EventID, p.Type, p.AccountID, p.Desc, p.EndDate, p.StartDate, p.Location, p.EventTypeID }).ToList();

                JavaScriptSerializer js = new JavaScriptSerializer();
                return (js.Serialize(evs));
            }
            catch (Exception)
            {

                return "";
            }
        }

        public String GetEventListByDate(int accountID, DateTime currentDate)
        {
            try
            {
                //var query=db.Events.Where(p=>(p.AccountID==accounTID && (p.StartDate<=eventDate && p.EndDate>=eventDate )));
                var query = db.Events.Where(p => p.AccountID == accountID);
                //query = db.Events.Where(p => p.StartDate.Date >= eventDate.Date && p.EndDate.Date <= eventDate.Date);
                var evs = query.Select(p => new { p.EventID, p.Type, p.AccountID, p.Desc, p.EndDate, p.StartDate, p.Location, p.EventTypeID }).ToList();
                for (int i = 0; i < evs.Count; i++)
                {
                    if (!(evs[i].StartDate.Date <= currentDate.Date && currentDate.Date <= evs[i].EndDate.Date))
                    {
                        evs.RemoveAt(i);
                        i--;

                    }

                }

                JavaScriptSerializer js = new JavaScriptSerializer();

                return js.Serialize(evs);

            }
            catch (Exception)
            {

                return "";
            }


        }


    }
}